package com.feilong.edgeAI.vm;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;

import com.aiibt.common.bean.AppVersion;
import com.aiibt.common.core.DataCall;
import com.aiibt.common.core.WDViewModel;
import com.aiibt.common.core.exception.ApiException;
import com.aiibt.common.util.Constant;
import com.blankj.utilcode.util.SPUtils;
import com.feilong.edgeAI.R;
import com.feilong.edgeAI.request.IMainRequest;
import com.feilong.edgeAI.update.ApkUpdateService;
import com.warkiz.tickseekbar.OnSeekChangeListener;
import com.warkiz.tickseekbar.SeekParams;
import com.warkiz.tickseekbar.TickSeekBar;

/**
 * Created by【仿脑科技】
 *
 * @Author: potato
 * @CreateDate: 2023/12/20 15:37
 */
public class SaveViewModel extends WDViewModel<IMainRequest> {
    public MutableLiveData<AppVersion> result = new MutableLiveData<>();
    public MutableLiveData<Float> scaleValue = new MutableLiveData<>();
    public void goBlueToochDevices() {
        intentByRouter(Constant.ACTIVITY_URL_DEVICELIST);
    }
    public void goAwake() {
        intentByRouter(Constant.ACTIVITY_URL_AWAKE);
    }
    public void goAbout() {
        intentByRouter(Constant.ACTIVITY_URL_ABOUT);
    }
    public void goSave() {
        intentByRouter(Constant.ACTIVITY_URL_HELP);
    }


    public void requestLastVersion(Context context,boolean isClick) {
        request(iRequest.appLastVersion(), new DataCall<AppVersion>() {
            @Override
            public void success(AppVersion data) {
                result.setValue(data);
                AppVersion appVersion = data;
                int apkCode = Integer.valueOf(appVersion.getVersionCode());
                try {
                    int versionCode = context.getPackageManager().getPackageInfo(
                            context.getPackageName(), 0).versionCode;
                    if (apkCode > versionCode) {
                        String apkUrl = "https://cdn.aiibt.cn"+appVersion.getPath();
                        showDownLoad(context,appVersion.getDesc(),apkUrl);
                    }else{
                        if(isClick){
                            Toast.makeText(context, R.string.lastest_version,Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
                Log.d("MainViewModel", "success: "+data);
            }

            @Override
            public void fail(ApiException data) {
                Log.d("MainViewModel", "ApiException: "+data);
            }
        });
    }

    public void showDownLoad(Context context, String msg, final String url) {

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View v = layoutInflater.inflate(R.layout.update_dialog, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT)
                .setView(v).create();
        TextView contentTextView = v.findViewById(R.id.update_content);
        contentTextView.setText(msg);
        v.findViewById(R.id.update_confim_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ApkUpdateService.class);
                intent.putExtra(ApkUpdateService.APK_DOWNLOAD_URL, url);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(intent);
                } else {
                    context.startService(intent);
                }
                dialog.dismiss();
            }
        });
        v.findViewById(R.id.update_confim_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        try {

            dialog.show();
            dialog.setView(v, 0, 0, 0, 0);
            dialog.setCancelable(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static final String TTS_SEEK ="tts_speed";
    public static final String TTS_SCALE="TTS_SCALE";
    public void showTtsSpeed(Context context) {

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View v = layoutInflater.inflate(R.layout.tts_speed_dialog, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT)
                .setView(v).create();
        TickSeekBar seekBar = v.findViewById(R.id.seekbar);
        seekBar.customTickTexts(new String[]{"0.5","0.75","1","1.25","1.5"});
        int index = SPUtils.getInstance().getInt(TTS_SEEK,50);
        seekBar.setProgress(index);
        seekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {

            }

            @Override
            public void onStartTrackingTouch(TickSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(TickSeekBar seekBar) {

            }
        });
        v.findViewById(R.id.custom_btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float scale = 1;
                switch(seekBar.getProgress()){
                    case 0:
                        scale = 0.5f;
                        break;
                    case 25:
                        scale = 0.75f;
                        break;
                    case 50:
                        scale = 1;
                        break;
                    case 75:
                        scale = 1.25f;
                        break;
                    case 100:
                        scale = 1.5f;
                        break;
                }
                scaleValue.setValue(scale);
                SPUtils.getInstance().put(TTS_SEEK,seekBar.getProgress());
                SPUtils.getInstance().put(TTS_SCALE,scale);
                dialog.dismiss();
            }
        });
        v.findViewById(R.id.custom_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        try {

            dialog.show();
            dialog.setView(v, 0, 0, 0, 0);
            dialog.setCancelable(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
